(function (angular) {
    'use strict';

    angular.module('ytong.controllers').controller('loginCtrl',loginCtrl);

    function loginCtrl(authenticationService, $state, Constants, ModalService){

        this.username = undefined;
        this.password = undefined;

        this.login = function(){
            if(this.username !== undefined && this.password !== undefined){
                authenticationService.login(this.username, this.password)
                    .then(function(state){
                        $state.go(state);
                    });
            }
        };

        this.register = function(){
            ModalService.showModal({
                templateUrl: 'register',
                controller: 'registerUserController as ruc',

            })
                .then(function(modal){
                    modal.element.modal();
                    modal.close.then(function(result){

                        if(result.action === Constants.COMMANDS.ADD){
                            authenticationService.register(result.user);
                        }
                    });
                });
        };

    };


    angular.module('ytong.controllers').controller('registerUserController', registerUserController);

    function registerUserController(close){

        this.user = {};

        this.close = function(action) {
            var result = {};
            result.action = action;
            result.user = this.user;
            close(result, 500); // close, but give 500ms for bootstrap to animate
        };

    };
}(angular));